<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: login21.php');
    exit;
}

require_once 'department21.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $departmentModel = new Department();
    $name = $_POST['department_name'] ?? '';
    
    if (empty($name)) {
        $error = '系部名称不能为空';
    } else {
        try {
            $departmentModel->createDepartment($name);
            header("Location: departments21.php?success=系部添加成功");
            exit;
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教学服务系统 - 添加系部</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="header">
        <div class="container">
            <h1>教学服务系统</h1>
        </div>
    </div>

    <div class="main-nav">
        <div class="container">
            <ul>
                <li><a href="index.php">主页</a></li>
                <li><a href="departments21.php">系部管理</a></li>
                <li><a href="classes21.php">班级管理</a></li>
                <li><a href="students21.php">学生管理</a></li>
                <li><a href="teachers21.php">教师管理</a></li>
                <li><a href="courses21.php">课程管理</a></li>
                <li><a href="scores21.php">成绩管理</a></li>
                <li><a href="teachings21.php">授课管理</a></li>
                <li class="logout"><a href="logout21.php">退出登录</a></li>
            </ul>
        </div>
    </div>

    <div class="container">
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">添加系部</h2>
            </div>
            
            <?php if ($error): ?>
            <div class="message message-error"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label for="department_name">系部名称</label>
                        <input type="text" id="department_name" name="department_name" required
                               class="form-control" placeholder="请输入系部名称" maxlength="100">
                    </div>
                    
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">保存</button>
                        <a href="departments21.php" class="btn btn-secondary">取消</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>